/*
 * 浏览器环境状态管理
 * @Author: ljb
 * @Date: 2018-08-28 14:09:27
 * @Last Modified by: ljb
 * @Last Modified time: 2019-01-08 16:53:52
 */
import { formaturl } from 'src/utils/utils';

export default class StatusManagement {
	constructor() {
		const url_param = formaturl() || {};

		const ua = window.navigator.userAgent.toLowerCase();

		this.openid = url_param.openid;		// openid

		this.appid = url_param.appid;		// appid

		this.skin_detector_id = url_param.skin_detector_id;		// skin_detector_id

		this.favor_id = url_param.favor_id;		// 收藏用过的favor_id

		this.lang = url_param.lang;		// 语言参数lang

		this.customer_id = url_param.customer_id;		// 客户id

		this.store_id = url_param.store_id;		// 店铺id

		this.scan_event_id = url_param.scan_event_id;	// 扫码的scan_event_id

		this.mode = url_param.mode;			// 邮件访问模式

		this.is_wx_browser = /MicroMessenger/i.test(ua);		// 是否在微信浏览器

		this.is_ios_web = !!window.navigator.standalone;		// 是否在ios的web浏览器

		this.has_web_app_debug = 'web_app_debug' in url_param; // web_app_debug是在普通浏览器上模拟web桌面app测试用的参数

		this.is_web_chrome = window.matchMedia('(display-mode: standalone)').matches;	// 是否是web桌面app浏览器打开
	}

	// 用来获取实例
	static getInstance() {
		if (!this.instance) {
			this.instance = new StatusManagement();
		}
		return this.instance;
	}

	isWxBrowser() {
		return this.is_wx_browser;
	}

	isWebApp() {
		return this.is_ios_web || this.is_web_chrome || this.has_web_app_debug;
	}

	hasAppid() {
		return !!this.appid;
	}

	hasOpenid() {
		return !!this.openid;
	}

	hasFavorId() {
		return !!this.favor_id;
	}

	isScanMode() { // 存在scan_event_id代表是扫码进入的
		return !!this.scan_event_id;
	}

	isAppidWxMode() { // 微信浏览器且存在appid的模式
		return !!this.is_wx_browser && !!this.appid;
	}

	isDebugMode() { // 如果网址参数同时存在openid和appid，则证明处于本地调试模式
		return !!this.openid && !!this.appid;
	}
}
